<template>

  <div>

    <!-- 1.定义面包屑导航 -->
    <div id="mmx">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>会员中心</el-breadcrumb-item>
        <el-breadcrumb-item>用户信息</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 2.定义卡片视图 -->
    <el-card class="box-card">
      <!-- 3.定义栅格的一行 :gutter="20"定义行间距   :span="9" 占位符  -->
      <el-row :gutter="20">
        <el-col :span="9">
        </el-col>
      </el-row>
    </el-card>

    <div id="body">
      <el-row :gutter="20">
        <el-col :span="3">
          <el-upload class="avatar-uploader" :action="imageUrl" :on-success="handleAvatarSuccess" :data={id:userID}
            :show-file-list="false">
            <img :src="require(`../assets/images/${user.images}`)" class="avatar" />
          </el-upload>
        </el-col>

        <!-- 定义用户提交表单数据-->
        <el-col :span="21">
          <el-form :model="user" :rules="rules" ref="addUserRef" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="user.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="user.password" type="password" disabled></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
              <el-input v-model="user.phone" disabled></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="user.email" disabled></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <div style="padding-left: 230px;">
      <span slot="footer" class="dialog-footer" style="padding-left: 475px;">
        <el-button type="primary" @click="updateUserBtn">编辑</el-button>
      </span>
    </div>
    <!-- </el-dialog> -->

    <!-- 定义修改页面 当对话框关闭时,将表格数据重置
      <!-- 定义用户提交表单数据-->
    <div v-if="updateDialogVisible">
      <el-dialog title="修改信息" :visible.sync="updateDialogVisible" width="50%" @close="updateUserBtn">
        <el-form :model="user" :rules="rules" ref="updateUserRef" label-width="100px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="user.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="user.email"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateUser">确 定</el-button>
        </span>
      </el-dialog>
    </div>


  </div>
</template>

<script>
  export default {
    data() {
      const checkEmail = (rule, value, callback) => {
        const emailRege = /^[a-zA-Z0-9-_]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+$/
        if (emailRege.test(value)) {
          return callback()
        }
        callback(new Error('请填写正确的邮箱地址'))
      }
      const checkPhone = (rule, value, callback) => {
        const phoneRege = /^1[34578][0-9]{9}$/
        if (phoneRege.test(value)) {

          return callback()
        }
        callback(new Error('请填写正确的手机号'))
      }
      const checkPassword = (rule, value, callback) => {
        if (this.user.password !== value) return callback(new Error('2次密码输入不一致'))
        callback()
      }
      return {
        userID: JSON.parse(window.sessionStorage.getItem("rsp")).id,
        user: JSON.parse(window.sessionStorage.getItem("rsp")),
        imageUrl: 'http://localhost:8091/file/upload',
        dialogVisible: false,
        user: JSON.parse(window.sessionStorage.getItem("rsp")),
        rules: {
          username: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            }
          ],
          password2: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            },
            {
              validator: checkPassword,
              trigger: 'blur'
            }
          ],
          phone: [{
              required: true,
              message: '请输入手机号',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            },
            {
              validator: checkPhone,
              trigger: 'blur'
            }
          ],
          email: [{
              required: true,
              message: '请输入邮箱',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 30,
              message: '长度在 3 到 30 个字符',
              trigger: 'blur'
            },
            {
              validator: checkEmail,
              trigger: 'blur'
            }
          ]
        },
        updateDialogVisible: false,
      }
    },
    methods: {
      async handleAvatarSuccess(response, file) {
        this.user = response
        window.sessionStorage.setItem("rsp", JSON.stringify(response))
      },
      updateUserBtn() {
        //重置表格数据
        this.$refs.addUserRef.resetFields()
      },
      async updateUserBtn() {
        this.updateDialogVisible = true
        const {
          data: result
        } = await this.$http.post('/aj-user/fingOne', this.user)
        if (result.status !== 200) return this.$message.error("用户查询失败")
        this.user = result.data
      },
      closeUpdateDialog() {
        //重置表格数据
        this.$refs.updateUserRef.resetFields()
      },
      updateUser() {
        //1.预校验数据
        this.$refs.updateUserRef.validate(async valid => {
          if (!valid) return this.$message.error("表单验证没有通过")
          //根据接口文档要求封装数据
          let user = {}
          user.id = this.user.id
          user.phone = this.user.phone
          user.email = this.user.email
          const {
            data: result
          } = await this.$http.post(`/aj-user/updateUser`, user)
          if (result.status !== 200) return this.$message.error("用户修改失败")
          this.$message.success("用户更新成功")
          this.updateDialogVisible = false
          // this.$router.push("/index")

        })
      }

    },

  }
</script>


<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #ffffff;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #55ffff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  #mmx {
    padding-left: 470px;
  }

  #body {
    padding-left: 470px;
    /* padding-right: 500px; */
  }

  #body input {
    width: 700px;
  }
</style>
